<?php

declare(strict_types=1);

namespace App\Job;
use App\Model\Device;
use Hyperf\AsyncQueue\Job;

class IotEvent extends Job
{
    public $imei;
    public $messageResult;

    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 1;


    public function __construct($imei,$messageResult)
    {
        $this->imei = $imei;
        $this->messageResult = $messageResult;
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象

    }

    public function handle()
    {

        $device = Device::where('iot_sn', $this->imei)->first();
        if(!$device){
            $log_name = 'device' . date('Y-m-d', time());
            $loggerFactory = container()->get(\Hyperf\Logger\LoggerFactory::class);
            $logger = $loggerFactory->get($log_name, 'device');
            $logger->error('device['.$this->imei.'] not found -----'.date('Y/m/d H:i:s',time()));
        }
        $device->iotEventHandle($this->messageResult);;
    }
}
